package top.bluesword.laboratory.config;

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManagerFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EventListenerConfig {

  @Autowired
  private EntityManagerFactory entityManagerFactory;

  @PostConstruct
  public void init() {
    SessionFactoryImplementor sessionFactory = entityManagerFactory.unwrap(SessionFactoryImplementor.class);
    EventListenerRegistry eventListenerRegistry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
    eventListenerRegistry.getEventListenerGroup(EventType.POST_INSERT).appendListeners(new TestPostInsertEventListener());
  }

}
